package rd.hardcore.ui.pad 
{
	import caurina.transitions.Tweener;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	import fr.digitas.flowearth.conf.Conf;
	import rd.hardcore.engine.datas.streamBoard.StreamBoardEvent;
	import rd.hardcore.engine.datas.StreamBoardManager;
	import rd.hardcore.events.NavEvent;
	import rd.hardcore.Launcher;
	import rd.hardcore.params.GlobalParams;
	import rd.hardcore.ui.nav.Nav;
	import rd.hardcore.ui.pad.stream.StreamPadsManager;
	
	/**
	 * PadsManagers Container/Controller
	 * @author Romain Prache
	 */
	
	public class PadsManagerContainer extends Sprite
	{
		/* VAR */
		static public var displacing	: Boolean = false;
		
		
		// CLASSIC PADS MANAGERS LIST
		private var tabPadsManager		: Vector.<PadsManager>;
		// ACTIVE STREAM PADS MANAGERS LIST
		private var addTabPadsManager	: Vector.<StreamPadsManager>;
		
		private var trash				: Trash;
		private var nav					: Nav;
		private var currentManager		: PadsManager;
		private var oldManager			: PadsManager;
		private var limit				: int;
		private var navActiveIDs		: int;
		private var initX				: int;
		private var initY				: int = 82;
		
		private var streamManager		: StreamPadsManager;
		
		
		/* Pads Managers Container */
		public function PadsManagerContainer() 
		{
			nav = new Nav();
			nav.addEventListener(NavEvent.SWITCH_PADMANAGER, handleSwitchFromNav);
			addChild(nav);
			
			tabPadsManager 		= new Vector.<PadsManager>();
			addTabPadsManager 	= new Vector.<StreamPadsManager>();
			
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		
		/* Initialize */
		private function init(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);

			var i : int = 0;
			
			for each( var node : XML in Conf.getDatas("pads").children() ) 
			{
				var padsmanager : PadsManager = new PadsManager( i );
				padsmanager.build( node );
				
				tabPadsManager.push(padsmanager);
				
				if ( i == 0 ) currentManager = padsmanager;
				
				i++;
			}
			
			currentManager.x = initX = ((GlobalParams.WIDTH - currentManager.width) 	>> 1) - 10 ; //lol
			currentManager.y = initY;
			addChild(currentManager);
			
			oldManager = currentManager;
			
			// nav
			nav.build( tabPadsManager.length);
			
			//nav.x = ( ( width - nav.width ) >> 1 ) + 20; //lol again
			nav.x = ( width ) >> 1; 
			nav.y = 319;
			
			trash = new Trash();
			addChild(trash);		
			
			var stream : Boolean;
			
			// add StreamPadsManager
			switch(StreamBoardManager.instance.STATE) {
				case StreamBoardManager.NO_STREAM_BOARD:
					/* Il n'y a pas de planche	*/
					trace("2:NO STREAMBOARD!");
				break;
				case StreamBoardManager.PENDING:
					/*	Ecriture en court	*/
					StreamBoardManager.instance.addEventListener(StreamBoardEvent.BOARD_READY_TO_DISPLAY, buildStreamPadsManagers);
					stream = true;
				break;
				case StreamBoardManager.STREAM_BOARD_READY:
					/*	Fichiers et datas ready	*/
					buildStreamPadsManagers();
					stream = true;
				break;
			}
			
			// app Ready
			Launcher.getInstance().init();
			addEventListener(MouseEvent.MOUSE_WHEEL, handleWheel);
		}
		
		private function buildStreamPadsManagers(e:StreamBoardEvent = null):void
		{
			limit = tabPadsManager.length + StreamBoardManager.instance.STREAM_COUNT;
			
			refreshStreamPadsManagers();
			
			StreamBoardManager.instance.addEventListener( Event.CHANGE, refreshStreamPadsManagers);
			
		}
		
		private function refreshStreamPadsManagers ( e : Event = null  ) : void
		{
			// dispose
			var i : int;
			
			// ouverture du 1st board
			if (currentManager.ID > (tabPadsManager.length - 1)) {
				nav.manageSelection(0);	
				switchKeyBoards(0);
			}
			
			for ( i = 0; i < addTabPadsManager.length; i ++ )
				addTabPadsManager[i].dispose();
			
			addTabPadsManager = new Vector.<StreamPadsManager>();
			
			navActiveIDs = tabPadsManager.length;
			
			for (i = tabPadsManager.length; i < limit; i++)
			{
				if (StreamBoardManager.instance.isActiveById( i ) )
				{
					streamManager = new StreamPadsManager( i );
					streamManager.build( StreamBoardManager.instance.getPadsXML() ); // recup XML de 
					addTabPadsManager.push( streamManager );
					navActiveIDs ++;
				}
			}
			
			nav.additionalBuild(addTabPadsManager.length);
						
		}
		
		
		/* Key Board Controller */
		public function handleKeyDown(evt : KeyboardEvent ) : void
		{	
			if ( evt.keyCode != Keyboard.LEFT && evt.keyCode != Keyboard.RIGHT )
				currentManager.manageKeyDown(evt);
		}
		
		/* Frow wheel ... */
		public function handleWheel(e:MouseEvent):void 
		{
			if (displacing) return;
			
			var newID : int;
			
			// LEFT
			if ( e.delta > 0 )
			{
				newID = currentManager.ID - 1;
				if ( newID < 0 ) newID = navActiveIDs -1;
				
			// RIGHT
			} else {
				
				newID = ( currentManager.ID + 1 ) % navActiveIDs;
				if ( newID > navActiveIDs ) newID = 0; // nique les modulos
				
			}
			
			nav.manageSelection(newID);
			
			switchKeyBoards( newID );
		}
		
		
		/* From Click ... */
		private function handleSwitchFromNav(e:NavEvent):void 
		{
			if (displacing) return;
			
			switchKeyBoards( e.id );
		}

		
		private function switchKeyBoards( _id : int ) : void
		{
			// Classic Boards
			if( _id < tabPadsManager.length)
				currentManager = tabPadsManager[_id];
			// Funky Boards	
			else
				currentManager = addTabPadsManager[ _id - tabPadsManager.length ];
				
			displacing = true;	
			
			currentManager.x = initX;
			currentManager.y = initY;
			currentManager.alpha = 0;
			addChildAt(currentManager,0);
						
			if ( currentManager is StreamPadsManager ) 
				trash.hide();
			else
				trash.display();
			
			
			Tweener.addTween( oldManager, 		{ alpha:0,	time:.4 } );
			Tweener.addTween( currentManager, 	{ alpha:1,	time:.4, onComplete:completeSwitch } );			
		}
		
		private function completeSwitch() : void
		{
			removeChild(oldManager);
			
			oldManager = currentManager;
			displacing = false;
		}
		
	}

}